/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.forms.searchResult;

import constants.UiConstants;

import javax.swing.*;
import java.awt.event.*;

/**
 * Class for managing dynamic divider position in JSplitPane
 * on resize and state change events
 *
 * @author rymek
 *         User: rymek
 *         Date: 31.08.11
 *         Time: 12:20
 */
class SearchResultFormWindowListener implements WindowStateListener, ComponentListener {

    private final SearchResultForm searchResultForm;  // parent form

    /**
     * Constructor.
     * Also sets reference to parent SearchResultForm
     *
     * @param searchResultForm parent SearchResultForm
     */
    public SearchResultFormWindowListener(SearchResultForm searchResultForm) {
        this.searchResultForm = searchResultForm;
    }

    /**
     * Adjusts DividerLocation for specified JSplitPane
     *
     * @param jSplitPane JSplitPane to adjust
     * @param proportion proportions
     */
    private void setDividerLocation(final JSplitPane jSplitPane,
                                    final double proportion) {
        if (jSplitPane.isShowing()) {
            if (jSplitPane.getWidth() > 0 && jSplitPane.getHeight() > 0) {
                jSplitPane.setDividerLocation(proportion);
            } else {
                jSplitPane.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent ce) {
                        jSplitPane.removeComponentListener(this);
                        setDividerLocation(jSplitPane, proportion);
                    }
                });
            }
        } else {
            jSplitPane.addHierarchyListener(new HierarchyListener() {
                @Override
                public void hierarchyChanged(HierarchyEvent e) {
                    if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 &&
                            jSplitPane.isShowing()) {
                        jSplitPane.removeHierarchyListener(this);
                        setDividerLocation(jSplitPane, proportion);
                    }
                }
            });
        }
    }

    /**
     * Handles maximize events for jframe
     *
     * @param e WindowState event
     */
    @Override
    public void windowStateChanged(WindowEvent e) {
        this.setDividerLocation(this.searchResultForm.getResultSplitPane(), UiConstants.DEFAULT_SPLIT_PANE_DIVIDER_LOC);
    }

    /**
     * Handles resize events for jframe
     *
     * @param e Component event
     */
    @Override
    public void componentResized(ComponentEvent e) {
        this.setDividerLocation(this.searchResultForm.getResultSplitPane(), UiConstants.DEFAULT_SPLIT_PANE_DIVIDER_LOC);
    }

    /**
     * Handles Show events for JFrame
     *
     * @param e Component event
     */
    @Override
    public void componentShown(ComponentEvent e) {
        this.setDividerLocation(this.searchResultForm.getResultSplitPane(), UiConstants.DEFAULT_SPLIT_PANE_DIVIDER_LOC);
    }

    @Override
    public void componentHidden(ComponentEvent e) {
    }

    @Override
    public void componentMoved(ComponentEvent e) {

    }
}
